package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageSerivce;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
public class SpuManageController {
    @Autowired
    private ManageSerivce manageSerivce;
//    http://api.gmall.com/admin/product/{page}/{limit}?category3Id=61
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 String category3Id){
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        IPage<SpuInfo> iPage = manageSerivce.getSpuInfoList(spuInfoPage,category3Id);
        return Result.ok(iPage);
    }
    //http://api.gmall.com/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageSerivce.getBaseAttrList();
        return Result.ok(baseSaleAttrList);
    }
    //http://api.gmall.com/admin/product/saveSpuInfo
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageSerivce.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    /**
     * 根据spuId查询spuImageList
     *
     * http://api.gmall.com/admin/product/spuImageList/{spuId}
     */
    @GetMapping("spuImageList/{spuId}")
    public Result<List<SpuImage>> spuImageList(@PathVariable Long spuId){
        return Result.ok(manageSerivce.getSpuImageList(spuId));
    }
    /**
     * 根据spuId 查询销售属性集合
     * @param spuId
     * @return
     * http://api.gmall.com/admin/product/spuSaleAttrList/{spuId}
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageSerivce.getSpuSaleAttrList(spuId);;
        return Result.ok(spuSaleAttrList);
    }
}
